package com.stu.Controller;

import com.stu.Pojo.Shop;
import com.stu.service.ShopService;
import com.stu.service.impl.ShopServiceiml;
import com.stu.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopCtroller {
    @Autowired
    private ShopServiceiml shopServiceiml;


    @GetMapping
    public List<Shop>queryAll(){ return shopServiceiml.selectALL(); }
    @GetMapping("/id")
    public Shop queryById(@PathVariable("id")Long id){//前端传id过来--@PathVariable
        return shopServiceiml.selectByid(id);
    }
    @PutMapping
    public AjaxResult AddOrInsert(@RequestBody Shop shop){
        try {
            if (shop.getId() ==null ){
               shopServiceiml.insert(shop);
            }else{
                shopServiceiml.update(shop);
            }
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"操作失败，请重试！");
        }
        return new AjaxResult(true,"操作成功！");
    }
    @DeleteMapping("/shop")
    public AjaxResult removeById(@PathVariable("id")Long id) {//前端传过的id---Long id
        try{
            shopServiceiml.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"删除失败，请重试!!");
        }
        return new AjaxResult(true,"删除成功!!");
    }
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try{
           shopServiceiml.batchRemove(ids);
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"删除失败，请重试!!");
        }
        return new AjaxResult(true,"删除成功!!");


    }
}
